package com.situ.mall.controller.back;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.pojo.bo.ProductSearchBO;
import com.situ.mall.pojo.entity.Category;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.JSONResult;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private IProductService productService;
	@Autowired
	private ICategoryService categoryService;
	
	@ResponseBody
	@RequestMapping("/selectByPage.action")
	public JSONResult selectByPage(Integer page, Integer limit, ProductSearchBO productSearchBO) {
		JSONResult jsonResult = productService.selectByPage(page, limit, productSearchBO);
		return jsonResult;
	}
	
	@RequestMapping("/getProductListPage.action")
	public String getProductListPage() {
		return "back/product_list";
	}
	
	@RequestMapping("/getProductInsert.action")
	public String getProductInsert(Model model) {
		List<Category> topCategoryList = categoryService.selectTopCategory();
		model.addAttribute("topCategoryList", topCategoryList);
		return "back/product_insert";
	}
	
	@ResponseBody
	@RequestMapping("/selectSecondCategoryList.action")
	public JSONResult selectSecondCategory(Integer topCategoryId) {
		List<Category> secondCategory = categoryService.selectSecondCategory(topCategoryId);
		return JSONResult.success(secondCategory);
	}
	
	@ResponseBody
	@RequestMapping("/deleteByPrimaryKey.action")
	public JSONResult deleteByPrimaryKey(Integer id) {
		System.out.println(id);
		boolean isSuccess = productService.deleteByPrimaryKey(id);
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = JSONResult.success("删除成功");
		} else {
			jsonResult = JSONResult.error("删除失败");
		}
		return jsonResult;
	}
	
	@ResponseBody
	@RequestMapping("/deleteAll.action")
	public JSONResult deleteAll(@RequestParam(value = "ids[]")Integer[] ids) {
		System.out.println(ids);
		boolean isSuccess = productService.deleteAll(ids);
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = JSONResult.success("删除成功");
		} else {
			jsonResult = JSONResult.error("删除失败");
		}
		return jsonResult;
	}
	
	@ResponseBody
	@RequestMapping("/insert.action")
	public JSONResult insert(Product product) {
		boolean isSuccess = productService.insert(product);
		return isSuccess ? JSONResult.success("添加成功") : JSONResult.error("添加失败");
	}
	
	@RequestMapping("/selectProductById.action")
	public String selectProductById(Integer id, Model model) {
		Product product = productService.selectProductById(id);
		Integer parentId = categoryService.selectParentIdByProduct(product.getCategoryId());
		List<Category> secondCategory = categoryService.selectSecondCategory(parentId);
		for (Category category : secondCategory) {
			System.out.println(category);
		}
		List<Category> topCategoryList = categoryService.selectTopCategory();
		model.addAttribute("parentId", parentId);
		model.addAttribute("product", product);
		model.addAttribute("topCategoryList", topCategoryList);
		model.addAttribute("secondCategory", secondCategory);
		return "back/product_update";
	}
	
	@ResponseBody
	@RequestMapping("/update.action")
	public JSONResult update(Product product) {
		System.out.println(product);
		boolean isSuccess = productService.update(product);
		return isSuccess ? JSONResult.success("修改成功") : JSONResult.error("添加失败");
	}
}
